package main

import (
	"bytes"
	"context"
	"encoding/json"
	"github.com/golang/protobuf/proto"
	"github.com/golang/protobuf/ptypes/empty"
	"github.com/valyala/fasthttp"
	"google.golang.org/grpc/metadata"
	"kop/pb"
	"log"
	"net/http"
	"time"
)

func main() {

	var msg, _ = proto.Marshal(&empty.Empty{})

	var req fasthttp.Request
	req.SetRequestURI("http://127.0.0.1:10000/Stage/Walk")
	req.Header.Add("Token", "05c76b3f5847d9caae5c5855edb3ee31")
	req.SetBody(msg)
	req.Header.SetContentType("application/protobuf")
	req.Header.SetMethod("POST")

	var resp fasthttp.Response

	var err = fasthttp.Do(&req, &resp)
	if err != nil {
		panic(err)
	}

	if resp.StatusCode() != http.StatusOK {
		log.Println(resp.Header.StatusCode(), string(resp.Body()))
	}

	var reply pb.WalkReply
	_ = proto.Unmarshal(resp.Body(), &reply)

	var b, _ = json.Marshal(reply)

	var str bytes.Buffer
	_ = json.Indent(&str, b, "", "    ")

	log.Println(str.String())
}

func ctx(token string) (context.Context, context.CancelFunc) {
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	ctx = metadata.NewOutgoingContext(ctx, metadata.New(map[string]string{"token": token}))
	return ctx, cancel
}
